<template>
  <div>
    <el-dialog
      :visible.sync="dialogFormVisible"
      width="23%"
      @close="exportShowDialog"
      :exportShow="exportShow"
    >
      <el-form :model="exportForm" ref="exportFormRef" :rules="exportRules">
        <el-form-item label="活动名称" prop="activityId">
          <el-select
            size="mini"
            v-model="exportForm.activityId"
            placeholder="请选择活动名称"
          >
            <el-option
              v-for="item in activityOptions"
              :key="item.activityId"
              :label="item.activityName"
              :value="item.activityId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择人群" prop="activityCrowd">
          <el-select
            size="mini"
            v-model="exportForm.activityCrowd"
            placeholder="请选择人群"
          >
            <el-option
              v-for="item in crowdOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="exportShowDialog">取 消</el-button>
        <el-button type="success" @click="exportConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllActivity } from '@/api/invitedRecord/index'

export default {
  name: 'exportDialog',
  props: {
    exportShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      exportForm: {
        activityId: '',
        activityCrowd: '1'
      },
      exportRules: {
        activityId: [
          { required: true, message: '请选择活动名称', trigger: 'blur' }
        ],
        activityCrowd: [
          { required: true, message: '请选择人群', trigger: 'blur' }
        ]
      },
      dialogFormVisible: this.exportShow,
      // 选择人群
      crowdOptions: [
        {
          value: '1',
          label: '邀请人'
        },
        {
          value: '2',
          label: '被邀请人未领课'
        },
        {
          value: '3',
          label: '被邀请人已领课'
        }
      ],
      // 活动名称
      activityOptions: []
    }
  },
  methods: {
    // 点击确定
    exportConfirm() {
      this.exportFileAll()
    },
    /*全部导出 */
    exportFileAll() {
      this.$refs.exportFormRef.validate((valid) => {
        if (valid) {
          this.$confirm('确定导出转介绍邀请记录？', '系统提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              let params = { crowd: this.exportForm.activityCrowd }
              let url = `/activities/recommends-record/${this.exportForm.activityId}/export`
              this.downFile('get', url, params, '转介绍邀请记录')
            })
            .catch(() => {})
            .finally(() => {
              this.$emit('update:exportShow', false)
            })
        }
      })
    },
    // 关闭dialog
    exportShowDialog() {
      this.exportForm = {
        activityId: '',
        activityCrowd: '1'
      }
      this.$emit('update:exportShow', false)
    },
    // 获取全部活动
    getAllActivity() {
      let params = ''
      getAllActivity(
        params,
        (res) => {
          this.activityOptions = res
        },
        (err) => {
          console.log(err)
        }
      )
    }
  },
  created() {
    this.getAllActivity()
  },
  mounted() {},
  watch: {
    exportShow() {
      this.dialogFormVisible = this.exportShow
    }
  }
}
</script>

<style lang="less" scoped>
.dialog-footer .el-button {
  flex: 1;
}
:deep(.el-form .el-form-item) {
  margin-bottom: 16px !important;
}
</style>
